package com.iemes.controller.equip;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.EquipChekcontentFormMap;
import com.iemes.entity.HandleFormMap;
import com.iemes.entity.HoistcheckFormMap;
import com.iemes.entity.ResFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.equip.EquipChekcontentMapper;
import com.iemes.mapper.equip.HandleMapper;
import com.iemes.mapper.equip.HoistcheckMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@Controller
@RequestMapping("equip")
public class HoistcheckController extends BaseController {
	@Inject
	private HoistcheckMapper hoistcheck;
	@Inject
	private HandleMapper handleMapper;
	@Inject
	private EquipChekcontentMapper chekcontent;

	@RequestMapping(value = "equip_hoist", method = RequestMethod.GET, produces = "text/html; charset=utf-8")
	public String examinepru(Model model) {
		List<ResFormMap> findByRes = findByRes();
		model.addAttribute("res", findByRes);
		return Common.BACKGROUND_PATH + "/equip/hoistcheck/equipbom_list";

	}

	@ResponseBody
	@RequestMapping("equip_hoistByPage")
	public PageView findByPage(String pageNow, String pageSize) {
		HoistcheckFormMap hoistcheckFormMap = getFormMap(HoistcheckFormMap.class);
		if (hoistcheckFormMap.get("status")==null) {
			hoistcheckFormMap.put("status", 1);
		}
		List<Map<String,Object>> equipHois = hoistcheck.getEquipHois(hoistcheckFormMap);
		try {
			LuogicPaging(pageNow, pageSize, equipHois);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return pageView;
	}

	@RequestMapping("equip_hoistAdd")
	public String addUI(Model model) throws Exception {
		EquipChekcontentFormMap equipChekcontentFormMap = getFormMap(EquipChekcontentFormMap.class);
		equipChekcontentFormMap.put("status", 1);
		equipChekcontentFormMap.put("formCategory", 1);
		List<Map<String,Object>> equipHandle =  chekcontent.getEquipChekc(equipChekcontentFormMap);
		model.addAttribute("equipHandle", equipHandle);
		return Common.BACKGROUND_PATH + "/equip/hoistcheck/equipbom_add";
	}

	@ResponseBody
	@RequestMapping("equipbom_hoistaddEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module = "设备电葫芦日常保养点检记录表", methods = "设备-新增点检记录表")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public synchronized String addEntity() throws Exception {
		HoistcheckFormMap hoistcheckFormMap = getFormMap(HoistcheckFormMap.class);
		HandleFormMap handFormMap = getFormMap(HandleFormMap.class);
		if (hoistcheckFormMap == null || hoistcheckFormMap.size() == 0) {
			return "false";
		}
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		String[] ContentId=request.getParameterValues("handleForm.id");
		String[] checkResult=request.getParameterValues("handleForm.checkResult");
		String[] handle=request.getParameterValues("handleForm.handle");
		String[] handleTime=request.getParameterValues("handleForm.handleTime");
		String[] repairPeopleId=request.getParameterValues("handleForm.repairPeopleId");
		String[] repairResult=request.getParameterValues("handleForm.repairResult");
		String[] remark=request.getParameterValues("handleForm.remark");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		hoistcheckFormMap.set("status", 1);
		hoistcheckFormMap.set("checkTime", sdf.format(date));
		hoistcheckFormMap.set("judgePeopleId", userFormMap.get("id"));
		hoistcheck.addEntity(hoistcheckFormMap);
		//得到返回的ID	
		for (int i = 0; i < ContentId.length; i++) {
			handFormMap.put("checkId", hoistcheckFormMap.get("id"));
			handFormMap.put("equipmentCheckContentId", ContentId[i]);
			handFormMap.put("checkResult", checkResult[i]);
			handFormMap.put("handle", handle[i]);
			if (Common.isNotEmpty(repairPeopleId[i])) {
				handFormMap.put("repairPeopleId", repairPeopleId[i]);
			}
			if (Common.isNotEmpty(handleTime[i])) {
				handFormMap.put("handleTime", handleTime[i]);
			}		
			handFormMap.put("repairResult", repairResult[i]);
			handFormMap.put("remark", remark[i]);
			handFormMap.put("supervisorId", userFormMap.get("id"));
			handleMapper.addEntity(handFormMap);
			handFormMap.clear();
		}				
		return "success";
	}

	@RequestMapping("hoistents_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		HoistcheckFormMap findbyFrist = hoistcheck.findbyFrist("id", id,
				HoistcheckFormMap.class);
		//保养点检不合格处理表
		List<Map<String,Object>> equipHandle = handleMapper.getEquipHandle("1",id);
		model.addAttribute("equipHandle", equipHandle);
		model.addAttribute("equipAll", findbyFrist);
		return Common.BACKGROUND_PATH + "/equip/hoistcheck/equipbom_edit";
	}

	@ResponseBody
	@Transactional(readOnly=false)
	@RequestMapping(value = "hoistent_editEntity")
	@SystemLog(module = "设备电葫芦日常保养点检记录表", methods = "设备-修改点检记录表")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() throws Exception {
		HoistcheckFormMap hoistcheckFormMap = getFormMap(HoistcheckFormMap.class);
		//保养点检不合格处理表
		HandleFormMap handleFormMap = getFormMap(HandleFormMap.class);
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean supervisorId 
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		String[] handleId=request.getParameterValues("handleForm.id");
		String[] checkResult=request.getParameterValues("handleForm.checkResult");
		String[] handle=request.getParameterValues("handleForm.handle");
		String[] handleTime=request.getParameterValues("handleForm.handleTime");
		String[] repairPeopleId=request.getParameterValues("handleForm.repairPeopleId");
		String[] repairResult=request.getParameterValues("handleForm.repairResult");
		String[] remark=request.getParameterValues("handleForm.remark");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		hoistcheckFormMap.put("date", sdf.format(date));
		hoistcheckFormMap.put("judgePeopleId", userFormMap.get("id"));
		hoistcheck.editEntity(hoistcheckFormMap);	
		for (int i = 0; i < handleId.length; i++) {
			handleFormMap.put("supervisorId", userFormMap.get("id"));
			handleFormMap.put("id", handleId[i]);
			handleFormMap.put("checkResult", checkResult[i]);
			handleFormMap.put("handle", handle[i]);
			if (Common.isNotEmpty(repairPeopleId[i])) {
				handleFormMap.put("repairPeopleId", repairPeopleId[i]);
			}
			if (Common.isNotEmpty(handleTime[i])) {
				handleFormMap.put("handleTime", handleTime[i]);
			}		
			handleFormMap.put("repairResult", repairResult[i]);
			handleFormMap.put("remark", remark[i]);
			handleFormMap.put("supervisorId", userFormMap.get("id"));
			handleMapper.editEntity(handleFormMap);
			handleFormMap.clear();
		}	
		return "success";
	}

	@ResponseBody
	@RequestMapping(value = "hoistent_deleteEntity")
	@SystemLog(module = "设备电葫芦日常保养点检记录表", methods = "设备-删除点检记录表")
	// 凡需要处理业务逻辑的.都需要记录操作日志category_adds
	public String deleteEntity() throws Exception {
		HoistcheckFormMap hoistcheckFormMap = getFormMap(HoistcheckFormMap.class);
		String[] ids = getParaValues("ids");
		String arr = ids[0];
		String[] str = arr.split(",");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		for (int i = 0; i < str.length; i++) {
			hoistcheckFormMap.put("date", sdf.format(date));
			hoistcheckFormMap.set("status", 2);
			hoistcheckFormMap.put("id", str[i]);
			hoistcheckFormMap.put("judgePeopleId", userFormMap.get("id"));
			hoistcheck.editEntity(hoistcheckFormMap);
		}
		return "success";
	}
	
}
